

*++++++++++++++ ITT LASSO Estimation +++++++++++++*
foreach domain_num of numlist 1 111 2 3 4 41 42 51 52 6 61 62 63 7 8 9 91 92 93 95 10 11 12 13 14 15 16 171 18 19 {
		
	***** Set outcome variables for each domain
	* var_list: outcomes in each domain; baseline_list: outcomes w baseline; nobaseline_list: outcomes w/o baseline

	if `domain_num' == 1 {
		local var_list "d1_1 d1_2 d1_3 d1_4 d1_5 d1_6 d1_7 d1_8"
		local baseline_list ""d1_1", "d1_2", "d1_3", "d1_4", "d1_5", "d1_6", "d1_7", "d1_8"" // inlist limits arguments to 9
		local nobaseline_list """"
	}
	
	if `domain_num' == 111 {
		local var_list "d1a_9 d1a_10 d1a_11 d1a_12 d1a_13 d1a_14"
		local baseline_list ""d1a_9", "d1a_10", "d1a_11""  // inlist limits arguments to 9
		local nobaseline_list ""d1a_12", "d1a_13", "d1a_14""
	}	
			
	if `domain_num' == 2 {
		local var_list "d2_1"
		local baseline_list ""d2_1""
		local nobaseline_list """"
	}
	
	if `domain_num' == 3 {
		local var_list "d3_1 d3_2 d3_3"
		local baseline_list ""d3_1", "d3_2", "d3_3""
		local nobaseline_list """"
	}
	
	if `domain_num' == 4 {
		local var_list "d4_1 d4_2 d4_3 d4_4"
		local baseline_list ""d4_1", "d4_2", "d4_3", "d4_4""
		local nobaseline_list """"
	}	

	if `domain_num' == 41 { // 4a
		local var_list "d4a_1 d4a_2 d4a_3 d4a_4"
		local baseline_list ""d4a_1", "d4a_2", "d4a_3", "d4a_4""
		local nobaseline_list """"
	}		

	if `domain_num' == 42 { // 4b
		local var_list "d4b_1 d4b_2 d4b_3 d4b_4"
		local baseline_list ""d4b_1", "d4b_2", "d4b_3", "d4b_4""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 51 { // 5a
		local var_list "d5a_1 d5a_2"
		local baseline_list ""d5a_1", "d5a_2""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 52 { // 5b
		local var_list "d5b_1"
		local baseline_list ""d5b_1""
		local nobaseline_list """"
	}			
	
	if `domain_num' == 6 {
		local var_list "d6_1 d6_2 d6_3 d6_4 d6_5 d6_6 d6_7"
		local baseline_list ""d6_1", "d6_2", "d6_3", "d6_4", "d6_5", "d6_6", "d6_7""
		local nobaseline_list """"
	}			

	if `domain_num' == 61 { // 6a
		local var_list "d6a_1 d6a_2 d6a_3 d6a_4"
		local baseline_list ""d6a_1", "d6a_2", "d6a_3", "d6a_4""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 62 { // 6b
		local var_list "d6b_1 d6b_2 d6b_3 d6b_4"
		local baseline_list ""d6b_1", "d6b_2", "d6b_3", "d6b_4""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 63 { // 6c
		local var_list "d6c_4 d6c_5"
		local baseline_list ""d6c_4", "d6c_5""
		local nobaseline_list """"
	}		
	
	if `domain_num' == 7 {
		local var_list "d7_1 d7_2 d7_3 d7_4 d7_5 d7_6 d7_7"
		local baseline_list ""d7_1", "d7_2", "d7_3", "d7_4", "d7_5", "d7_6", "d7_7""
		local nobaseline_list """"
	}		
	
	if `domain_num' == 8 {
		local var_list "d8_1 d8_2 d8_3"
		local baseline_list ""d8_1", "d8_2", "d8_3""
		local nobaseline_list """"
	}			
	
	if `domain_num' == 9 {
		local var_list "d9_2 d9_3 d9_4 d9_5 d9_6 d9_7 d9_8 d9_9 d9_10"
		local baseline_list ""d9_2", "d9_3", "d9_4", "d9_5", "d9_6", "d9_7", "d9_8", "d9_9", "d9_10""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 91 { // 9a
		local var_list "d9a_1 d9a_2 d9a_3 d9a_4 d9a_5 d9a_6 d9a_7"
		local baseline_list """"
		local nobaseline_list ""d9a_1", "d9a_2", "d9a_3", "d9a_4", "d9a_5", "d9a_6", "d9a_7""
	}
	
	if `domain_num' == 92 { // 9b
		local var_list "d9b_1 d9b_2 d9b_3"
		local baseline_list """"
		local nobaseline_list ""d9b_1", "d9b_2", "d9b_3""
	}	
	
	if `domain_num' == 93 { // 9c
		local var_list "d9c_1 d9c_2 d9c_3"
		local baseline_list """"
		local nobaseline_list ""d9c_1", "d9c_2", "d9c_3""
	}		
	
	if `domain_num' == 95 { // 9e
		local var_list "d9e_1 d9e_2 d9e_3 d9e_4 d9e_5"
		local baseline_list """"
		local nobaseline_list ""d9e_1", "d9e_2", "d9e_3", "d9e_4", "d9e_5""
	}		
	
	if `domain_num' == 10 {
		local var_list "d10_1 d10_2 d10_3 d10_4 d10_5 d10_6 d10_7 d10_8"
		local baseline_list ""d10_1", "d10_3", "d10_4", "d10_5", "d10_6", "d10_7", "d10_8""
		local nobaseline_list ""d10_2""
	}		
	
	if `domain_num' == 11 {
		local var_list "d11_1 d11_2 d11_3 d11_4"
		local baseline_list ""d11_1", "d11_2", "d11_3", "d11_4""
		local nobaseline_list """"
	}		
	
	if `domain_num' == 12 {
		local var_list "d12_1 d12_2"
		local baseline_list ""d12_1", "d12_2""
		local nobaseline_list """"
	}		
	
	if `domain_num' == 13 {
		local var_list "d13_1"
		local baseline_list ""d13_1""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 14 {
		local var_list "d14_1 d14_2 d14_3 d14_4 d14_5 d14_6 d14_7 d14_8"
		local baseline_list ""d14_1", "d14_2", "d14_3", "d14_4", "d14_5", "d14_6", "d14_7", "d14_8""
		local nobaseline_list """"
	}	
	
	if `domain_num' == 15 {
		local var_list "d15_1"
		local baseline_list ""d15_1""
		local nobaseline_list """"
	}		
	
	if `domain_num' == 16 {
		local var_list "d16_1"
		local baseline_list ""d16_1""
		local nobaseline_list """"
	}			
				
	if `domain_num' == 171 {
		local var_list "d17_1_female d17_2"
		local baseline_list ""d17_1_female", "d17_2""
		local nobaseline_list """"
	}				
	
	if `domain_num' == 18 {
		local var_list "d18_1 d18_2 d18_3 d18_4 d18_5"
		local baseline_list """"
		local nobaseline_list ""d18_1", "d18_2", "d18_3", "d18_4", "d18_5""
	}
	
	if `domain_num' == 19 {
		local var_list "d19_1 d19_2 d19_3 d19_4"
		local baseline_list """"
		local nobaseline_list ""d19_1", "d19_2", "d19_3", "d19_4""
	}

	***** Set baseline for outcome variables w baseline measure
	* baseline: baseline measure; missing: whether the measure is missing in baseline
	
	local num = 1 // count the # of regressions in each domain

	foreach var in `var_list' {
	
	***** Regression for outcomes w baseline measure
	if inlist("`var'",`baseline_list') {
		pdslasso `var' ib6.treatment (i.strata i.wave phone_survey survey_date missing_b`var' b`var' $cat_list $lik_list $con_list), partial(i.strata i.wave phone_survey survey_date missing_b`var' b`var') post(pds) robust cluster(ent_id) lopt(prestd)
		
		mat m`num' = r(table)
		forval i = 1/5 {
			local p`num'_`i' = m`num'[4, `i']	
		} // extract p-value
		test 4.treatment = 5.treatment
		estadd scalar p1 = r(p)
		test 3.treatment = 5.treatment
		estadd scalar p2 = r(p)
		test 2.treatment = 5.treatment
		estadd scalar p3 = r(p)
		test 2.treatment = 4.treatment
		estadd scalar p4 = r(p)
		test 1.treatment = 2.treatment
		estadd scalar p5 = r(p)
		estadd local p_blank = "" // to produce table
		
		if !inlist("`var'", "d2_1", "d9_2", "d9_3", "d9_5", "d10_1", "d10_3", "d18_1", "d18_5") { // non monetary variable
			qui sum b`var' if treatment == 6 & finish_flag == 1
			estadd scalar b_mean = r(mean)
			qui sum `var' if treatment == 6 & e(sample)
			estadd scalar post_mean = r(mean)			
		}
		
		if inlist("`var'", "d2_1", "d9_2", "d9_3", "d9_5", "d10_1", "d10_3", "d18_1", "d18_5") { // monetary variable + d9_3 (hours worked)
			qui sum b`var'_stat if treatment == 6 & finish_flag == 1
			estadd scalar b_mean = r(mean)	
			qui sum `var'_stat if treatment == 6 & e(sample)
			estadd scalar post_mean = r(mean)			
		}		
		eststo domain`domain_num'_`num'
		local ++num
	
	}	
	
	***** Regression for outcomes w/o baseline measure
	if inlist("`var'", `nobaseline_list') {
		pdslasso `var' ib6.treatment (i.strata i.wave phone_survey survey_date $cat_list $lik_list $con_list), partial(i.strata i.wave phone_survey survey_date) post(pds) robust cluster(ent_id) lopt(prestd)
		
		mat m`num' = r(table)
		forval i = 1/5 {
			local p`num'_`i' = m`num'[4, `i']	
		} // extract p-value
		test 4.treatment = 5.treatment
		estadd scalar p1 = r(p)
		test 3.treatment = 5.treatment
		estadd scalar p2 = r(p)
		test 2.treatment = 5.treatment
		estadd scalar p3 = r(p)
		test 2.treatment = 4.treatment
		estadd scalar p4 = r(p)
		test 1.treatment = 2.treatment
		estadd scalar p5 = r(p)
		
		estadd local p_blank = "" // to produce table
		estadd scalar b_mean = .
		
		if !inlist("`var'", "d2_1", "d9_2", "d9_3", "d9_5", "d10_1", "d10_3", "d18_1", "d18_5") { // non monetary variable
			qui sum `var' if treatment == 6 & e(sample)
			estadd scalar post_mean = r(mean)			
		}
		
		if inlist("`var'", "d2_1", "d9_2", "d9_3", "d9_5", "d10_1", "d10_3", "d18_1", "d18_5") { // monetary variable + d9_3 (hours worked)
			qui sum `var'_stat if treatment == 6 & e(sample)
			estadd scalar post_mean = r(mean)			
		}		
		eststo domain`domain_num'_`num'
		local ++num
	}
	
}

	***** Calculate sharpened-q value (within each domain)
	
	quietly gen float pval = .

	local pnum = `num' - 1 // # of regressions in each domain
	forval pi = 1/`pnum' {
			forval pj = 1/5 {
				replace pval = `p`pi'_`pj'' if _n == `pi' * 5 - 5 + `pj'
			}
		} // input p-values 

	do "$path/Code/sharpened_qvalues.do" // Anderson code

	local qnum = `pnum'

	forval qi = 1/`qnum' {
			forval qj = 1/5 {
				local q_loc = `qi' * 5 - 5 + `qj'
				local q`qi'_`qj' = bky06_qval in `q_loc'
			}
			mat q = (`q`qi'_1', `q`qi'_2', `q`qi'_3', `q`qi'_4', `q`qi'_5')
			mat colnames q = 1.treatment 2.treatment 3.treatment 4.treatment 5.treatment
			estadd matrix q: domain`domain_num'_`qi'
		} // output q-values to each regression
	
	
	***** Add Domain Summary *****

	if !(`domain_num' == 18 | `domain_num' == 19 | `domain_num' == 91 | `domain_num' == 92 | `domain_num' == 93 | `domain_num' == 95) {
		
			* residualize for lee bounds
			reg e_domain`domain_num' i.strata i.wave phone_survey survey_date b_domain`domain_num'
			predict lee_outcome_`domain_num', r	
		
		pdslasso e_domain`domain_num' ib6.treatment (i.strata i.wave phone_survey survey_date b_domain`domain_num' $cat_list $lik_list $con_list), partial(i.strata i.wave phone_survey survey_date b_domain`domain_num') post(pds) robust cluster(ent_id) lopt(prestd)
		
		qui sum b_domain`domain_num' if treatment == 6 & finish_flag == 1
		estadd scalar b_mean = 0
		
	}
	
	else {
		
	* residualize for lee bounds
	reg e_domain`domain_num' i.strata i.wave phone_survey survey_date
	predict lee_outcome_`domain_num', r
	
		pdslasso e_domain`domain_num' ib6.treatment (i.strata i.wave phone_survey survey_date $cat_list $lik_list $con_list), partial(i.strata i.wave phone_survey survey_date) post(pds) robust cluster(ent_id) lopt(prestd)
		
		estadd scalar b_mean = .

	}
		
		test 4.treatment = 5.treatment
		estadd scalar p1 = r(p)
		test 3.treatment = 5.treatment
		estadd scalar p2 = r(p)
		test 2.treatment = 5.treatment
		estadd scalar p3 = r(p)
		test 2.treatment = 4.treatment
		estadd scalar p4 = r(p)
		test 1.treatment = 2.treatment
		estadd scalar p5 = r(p)
		estadd local p_blank = "" // to produce table
	

		qui sum e_domain`domain_num' if treatment == 6 & e(sample)
		estadd scalar post_mean = r(mean)
		
		forvalues i=1/5{
			test `i'.treatment = 0
			local p`i' = r(p)
		}
		mat q = (`p1', `p2', `p3', `p4', `p5')
		mat colnames q = 1.treatment 2.treatment 3.treatment 4.treatment 5.treatment	
		estadd matrix q
		
		eststo sw_domain`domain_num'
		
		local num_components: word count `var_list' // to know how many cols in table	
		local num_vars = `num_components' + 1 // plus domain index
		local num_cols = `num_vars' + 1 // plus col with titles
		
		local blank_row = "" //for the prefoot
		forvalues i=1/`num_vars'{
			local blank_row "`blank_row' &"
		}

	esttab sw_domain`domain_num' domain`domain_num'_* 	using "$path/Output/Appendix_E/domain`domain_num'.tex", label collabels(none) replace nolines nonumber keep(1.treatment 2.treatment 3.treatment 4.treatment 5.treatment) cells(b(star fmt(%9.2f)) se(par fmt(%9.2f)) q(par([ ] ) fmt(%9.2f))) stats(N b_mean post_mean p1 p2 p3 p4 p5, fmt(%9.0fc %12.2fc %12.2fc %9.2f %9.2f %9.2f %9.2f %9.2f) labels("Observations" "Control Mean: Baseline" "Control Mean: Follow-Ups" "Labeled Grant = Info Only" "Labeled Grant = Grant Only" "Labeled Grant = R-Mentee" "R-Mentee = Info Only" "R-Mentee = U-Mentee")) coeflabels(1.treatment "Mentored by Ugandan" 2.treatment "Mentored by Refugee" 3.treatment "Grant Only" 4.treatment "Information Only" 5.treatment "Info. + Labeled Grant") order(5.treatment 4.treatment 3.treatment 2.treatment 1.treatment) substitute(\_ _ \$ $) ///
star(* 0.1 ** 0.05 *** 0.01) ///
prehead("\begin{table}[H]	\centering	\footnotesize	\caption{Full Set of Outcomes in Domain `domain_num'} \label{tab:domain`domain_num'}	\begin{tabular}{l*{`num_vars'}{>{\centering\arraybackslash}p{1.4cm}}}\toprule \toprule ") ///
posthead("\cmidrule{2-`num_cols'}") ///
prefoot("`blank_row' \\") ///
postfoot("\bottomrule \bottomrule \multicolumn{`num_cols'}{p{\linewidth}}{\footnotesize An observation is a surveyed respondent, with one per post-baseline survey round, in Uganda. Results estimated through ANCOVA regression with baseline controls selected through double-lasso. Standard errors clustered at the enterprise level in parentheses. Brackets display sharpened $ q $-values controlling the false discovery rate for individual pre-specified outcomes, and two-sided $ p $-values for summary indices. \sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\).}  \end{tabular} \\ \end{table}%")

	drop pval original_sorting_order rank bky06_qval // prepare to calculate sharpened q-value for the next domain 
}
